'use strict';

const { Controller } = require('egg');

class NewsController extends Controller {
  async index() {
    const { ctx } = this;
    try {
      ctx.body = await ctx.service.news.getNewsList();
    } catch (e) {
      ctx.body = e.message;
    }
  }

  async search() {
    const { ctx } = this;
    const { word } = ctx.params;
    try {
      ctx.body = await ctx.service.news.search(word);
    } catch (e) {
      ctx.body = e.message;
    }
  }

  async pick() {
    const { ctx } = this;
    const { year, month, date } = ctx.params;
    const dt = new Date();
    dt.setFullYear(Number(year));
    dt.setMonth(Number(month));
    dt.setDate(Number(date));
    try {
      ctx.body = await ctx.service.news.pick(dt);
    } catch (e) {
      ctx.body = e.message;
    }
  }
}

module.exports = NewsController;
